package com.fhao.forum.services.impl;

import com.fhao.forum.common.AppResult;
import com.fhao.forum.common.ResultCode;
import com.fhao.forum.dao.ArticleMapper;
import com.fhao.forum.exception.ApplicationException;
import com.fhao.forum.model.Article;
import com.fhao.forum.model.Board;
import com.fhao.forum.model.User;
import com.fhao.forum.services.IArticleService;
import com.fhao.forum.services.IBoardService;
import com.fhao.forum.services.IUserService;
import com.fhao.forum.utils.StringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {
    @Resource
    private ArticleMapper articleMapper;
    @Resource
    private IBoardService boardService;
    @Resource
    private IUserService userService;
    @Override
    public void create(Article article) {
        if(article == null || article.getBoardId() <= 0 || article.getUserId() <= 0
                || StringUtil.isEmpty(article.getContent())
                || StringUtil.isEmpty(article.getTitle())) {
            log.warn("ArticleServiceImpl create方法"+ ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        article.setVisitCount(0);
        article.setReplyCount(0);
        article.setLikeCount(0);
        article.setState((byte) 0);
        article.setDeleteState((byte) 0);
        Date date = new Date();
        article.setCreateTime(date);
        article.setUpdateTime(date);
        //动态插入帖子
        int articleRow = articleMapper.insertSelective(article);
        if(articleRow <= 0) {
            log.warn("ArticleServiceImpl create方法"+ ResultCode.FAILED_CREATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        //获取用户信息
        User user = userService.selectById(article.getUserId());
        //校验用户信息
        if(user == null) {
            log.warn("ArticleServiceImpl create方法"+ ResultCode.FAILED_CREATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        //添加用户发帖数
        userService.addOneArticleCountById(user.getId());

        //获取版块信息
        Board board = boardService.selectById(article.getBoardId());
        if(board == null) {
            log.warn("ArticleServiceImpl create方法"+ ResultCode.FAILED_CREATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        //添加版块帖子数量
        boardService.addOneArticleCountById(board.getId());

        log.info("ArticleServiceImpl 发帖成功");

    }

    @Override
    public List<Article> selectAll() {
        List<Article> articles = articleMapper.selectAll();
        //还是一样的,无需校验,谁用谁校验.
        return articles;
    }

    @Override
    public List<Article> selectAllByBoardId(Long boardId) {
        //参数校验
        if(boardId == null || boardId <= 0) {
            log.warn("ArticleServiceImpl selectAllByBoardId方法, "+ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //查询版块信息
        Board board = boardService.selectById(boardId);
        //检验版块是否存在
        if(board == null) {
            log.warn("ArticleServiceImpl selectAllByBoardId方法, "+ResultCode.FAILED_BOARD_NOT_EXISTS);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        List<Article> articles = articleMapper.selectAllByBoardId(boardId);
        //services层不对结果作校验, 可能结果为空, 谁调用谁来校验即可.
        return articles;
    }

    @Override
    public List<Article> selectByUserId(Long userId) {
        //参数校验
        if(userId == null || userId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验用户是否存在
        User user = userService.selectById(userId);
        if(user == null) {
            log.warn(ResultCode.FAILED_USER_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        //调用DAO
        List<Article> articles = articleMapper.selectByUserId(userId);
        //返回结果
        return articles;
    }

    @Override
    public Article selectDetailById(Long id) {
        //参数校验
        if(id == null || id <= 0) {
            log.warn("ArticleServiceImpl selectDetailById方法, "+ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectDetailById(id);
        //根据该接口的实际功能来决定是否判断结果, 因为要增加访问量,所以就需要判断是否为空,
        if(article == null) {
            log.warn("ArticleServiceImpl selectDetailById方法, "+ResultCode.FAILED_ARTICLE_NOT_EXISTS);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        //重新new一个 article对象,用来更新数据库中帖子的访问量
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setVisitCount(article.getVisitCount()+1);
        //更新数据库中对应帖子的访问量
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1) {
            log.warn("ArticleServiceImpl selectDetailById方法, "+ResultCode.FAILED_ARTICLE_VISITCOUNT);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_VISITCOUNT));
        }
        //增加 帖子对象的访问量并返回给用户
        article.setVisitCount(article.getVisitCount()+1);
        return article;
    }

    @Override
    public Article selectById(Long id) {
        //参数校验
        if(id == null || id <= 0) {
            log.warn("ArticleServiceImpl selectDetailById方法, "+ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return articleMapper.selectByPrimaryKey(id);
    }

    @Override
    public void modify(Long id, String title, String content) {
        //参数校验
        if(id == null || id <= 0 || StringUtil.isEmpty(title)
                || StringUtil.isEmpty(content)) {
            log.warn("ArticleServiceImpl类 modify方法, "+ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article updateArticle = new Article();
        updateArticle.setId(id); //id
        updateArticle.setContent(content); //正文
        updateArticle.setTitle(title); //标题
        Date date = new Date();
        updateArticle.setUpdateTime(date); //更新时间
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1) {
            log.warn("ArticleServiceImpl类 modify方法, "+ResultCode.ERROR_SERVICES);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //没有抛异常说明更新成功.
    }

    @Override
    public void thumbsUpById(Long id) {
        //参数校验
        if(id == null || id <= 0) {
            log.warn("ArticleServiceImpl thumbsUpById方法, "+ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getDeleteState() == 1) {
            log.warn("ArticleServiceImpl thumbsUpById方法, "+ResultCode.FAILED_BOARD_NOT_EXISTS);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        if(article.getState() == 1) {
            log.warn("ArticleServiceImpl thumbsUpById方法, "+ResultCode.FAILED_ARTICLE_BANNED);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setLikeCount(article.getLikeCount()+1);
        updateArticle.setUpdateTime(new Date());
        //调用DAO
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1) {
            log.warn("ArticleServiceImpl thumbsUpById方法, "+ResultCode.ERROR_SERVICES);
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void deleteById(Long id) {
        //参数校验
        if(id == null || id <= 0) {
            log.warn("ArticleServiceImpl deleteById方法, "+ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getDeleteState() == 1) {
            log.warn("ArticleServiceImpl deleteById方法, "+ResultCode.FAILED_BOARD_NOT_EXISTS);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setDeleteState((byte) 1);
        //调用DAO
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES.toString()));
        }
        //更新用户发帖数
        userService.subOneArticleCountById(article.getUserId());
        //更新版块帖子数
        boardService.subOneArticleCountById(article.getBoardId());

        log.info("删除帖子成功, articleId = "+article.getId()+" userId = "+article.getUserId());
    }

    @Override
    public void addOneReplyCountById(Long id) {
        //参数校验
        if(id == null || id <= 0) {
            log.warn("ArticleServiceImpl deleteById方法, "+ResultCode.FAILED_PARAMS_VALIDATE);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleMapper.selectByPrimaryKey(id);
        if(article == null || article.getDeleteState() == 1) {
            log.warn("ArticleServiceImpl addOneReplyCountById方法, "+ResultCode.FAILED_ARTICLE_NOT_EXISTS);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if(article.getState() == 1) {
            log.warn("ArticleServiceImpl addOneReplyCountById方法, "+ResultCode.FAILED_ARTICLE_BANNED);
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        Article updateArticle = new Article();
        updateArticle.setId(article.getId());
        updateArticle.setReplyCount(article.getReplyCount()+1);
        updateArticle.setUpdateTime(new Date());

        //调用Dao
        int row = articleMapper.updateByPrimaryKeySelective(updateArticle);
        if(row != 1) {
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES.toString()));
        }

    }
}





















